<?php
namespace app\index\controller;

use app\common\model\Order;
use app\common\model\Pays;
use app\common\model\Users;
use app\common\model\Weblist;
use think\Db;

class CodePay extends Common
{

    private $userInfo;

    private $authController = array(
        'vip_Notify',
        'pe_Notify',
        'dl_Notify',
        'fz_Notify'
    );

    public function _empty()
    {
        exit($this->fetch('pc/common/empty'));
    }

    public function submit($orderid, $type)
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $codepay_zsm = 'zsm.png'; //上传到/static/codepay/zsm.png 会自动使用赞赏码来收款
        $codepay_qqpay = 'qqpay.png'; //上传到/static/codepay/qqpay.png 会自动使用来收款
        $codepay_wxpay = 'wxpay.png'; //上传到/static/codepay/wxpay.png 会自动使用来收款
        $codepay_alipay = 'alipay.png'; //上传到/static/codepay/alipay.png 会自动使用来收款
        $qr = ''; //初始化一个默认的二维码
        if (!is_numeric($orderid)) exit('订单号不符合要求!');
        $row = model('Pays')->where('orderid', '=', $orderid)->find();
        if (!$row) exit('该订单号不存在，请返回来源地重新发起请求！');
		$codepay_path = "https://codepay.fateqq.com";
		if ($type == 'wxpay') {
            $typeName = '微信';
            $type = 3;
            if (is_file($codepay_wxpay)) { //如果赞赏码存在 则使用赞赏码
                $qr = $codepay_config['qrcode_url'] = $codepay_wxpay;
            } else if (is_file($codepay_zsm)) { //如果赞赏码存在 则使用赞赏码
                $qr = $codepay_config['qrcode_url'] = $codepay_zsm;
            }
        } else if ($type == 'qqpay' || $type == 'tenpay') {
            $typeName = 'QQ';
            $type = 2;
            if (is_file($codepay_qqpay)) { //如果赞赏码存在 则使用赞赏码
                $qr = $codepay_config['qrcode_url'] = $codepay_qqpay;
            }
        } else {
            $type = 1;
            $typeName = '支付宝';
            if (is_file($codepay_alipay)) { //如果赞赏码存在 则使用赞赏码
                $qr = $codepay_config['qrcode_url'] = $codepay_alipay;
            }
        }
        $price = $row['money'];
        $param = $orderid;
        $siteurl = get_Domain();
        $pay_id = real_ip();
        $data = array(
            "id" => $codepay_config['id'],//平台ID号
            "type" => $type,//支付方式
            "price" => $price,//原价
            "pay_id" => $pay_id, //可以是用户ID,站内商户订单号,用户名
            "param" => $param,//自定义参数
//            "https" => 1,//启用HTTPS
            "act" => $codepay_config['act'],
            "outTime" => $codepay_config['outTime'],//二维码超时设置
            "page" => $codepay_config['page'],//付款页面展示方式
            "return_url" => $siteurl . 'Code_Pay/' . $row['shop'] . 'Return.mz',//付款后附带加密参数跳转到该页面
            "notify_url" => $siteurl . 'Code_Pay/' . $row['shop'] . 'Notify.mz',//付款后通知该页面处理业务
            "style" => $codepay_config['style'],//付款页面风格
            "user_ip" => real_ip(),//用户IP
            "out_trade_no" => $param,//单号去重复
            "createTime" => time(),//服务器时间
            "chart" => strtolower('utf-8')//字符编码方式
            //其他业务参数根据在线开发文档，添加参数.文档地址:https://codepay.fateqq.com/apiword/
            //如"参数名"=>"参数值"
        );
        

        $back = create_link($data, $codepay_config['key']);
		
		switch ((int)$type) {
            case 1:
                $typeName = '支付宝';
                break;
            case 2:
                $typeName = 'QQ';
                break;
            default:
                $typeName = '微信';
        }
        $user_data = array(
            "return_url" => '/index/Code_Pay/' . $row['shop'] . '_Return.html',
            "type" => $type,
            "outTime" => $codepay_config["outTime"],
            "codePay_id" => $codepay_config["id"],
            "out_trade_no" => $param,
            "price" => $price,
            'money' => $price,
            'order_id' => $param,
            "subject" => $row['name']
        ); //传给网页JS去执行

        $codepay_json = send_get($back['url']);

        if (empty($codepay_json)) {
            $data['call'] = "callback";
            $data['page'] = "3";
            $back = create_link($data, $codepay_config['key']);
            $codepay_html = '<script src="' . $back['url'] . '"></script>';
        } else {
            $codepay_data = json_decode($codepay_json);
            $qr = $codepay_data ? $codepay_data->qrcode : '';
            $user_data["money"] = $codepay_data && $codepay_data->money ? $codepay_data->money : $price;
            $codepay_html = "<script>callback({$codepay_json})</script>";
        }
		
		$user_data["qrcode_url"] = $codepay_data ? $codepay_data->qrcode : '';

        $user_data["logShowTime"] = $user_data["qrcode_url"] ? 1 : 8 * 1000;
        $this->assign('codepay_config', $codepay_config);
        $this->assign('typeName', $typeName);
        $this->assign('codepay_path', $codepay_path);
        $this->assign('codepay_html', $codepay_html);
        $this->assign('type', $type);
        $this->assign('user_data', $user_data);
        $this->assign('price', $price);
        $this->assign('qr', $qr);
        return $this->fetch('/codepay/pay');
    }

    public function vip_Return()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('get.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }
        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!input('get.pay_no') || md5($sign . $codepay_config['key']) != input('get.sign')) { //不合法的数据 KEY密钥为你的密钥
            exit('验证失败！');
        } else { //合法的数据
            $out_trade_no = input('get.param');
            //交易号
            $trade_no = input('get.pay_no');
            $pay_order = Pays::findByOrderId($out_trade_no);
            $order = [
                'type' => '' . $pay_order['type'] . '',
                'orderid' => $out_trade_no,
                'trade_no' => $trade_no,
                'time' => date("Y-m-d H:i:s"),
                'name' => $pay_order['name'],
                'money' => $pay_order['money'],
                'status' => 2,
                'zid' => WEB_ID,
            ];
            if ($pay_order['status'] == 0) {
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $start_time = is_Vip_Day($pay_order['shopid']);
                //开通时间
                $vip_start = date("Y-m-d");
                //计算开通的vip时长
                if ($this->userInfo['viptime']) {
                    $vip_time = date("Y-m-d", strtotime("+" . $start_time . " day", strtotime($this->userInfo['viptime'])));
                } else {
                    $vip_time = date("Y-m-d", strtotime("+" . $start_time . " day"));
                }
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update([
                        'vipstart' => $vip_start,
                        'viptime' => $vip_time
                    ]);
            } else {
                $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
                $this->assign('url', '/pc/index/act/user.html');
                if(check_mobile()){
					exit($this->fetch('mobile/common/msg'));
				}else{
					exit($this->fetch('pc/common/msg'));
				}
            }
            $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
            $this->assign('url', '/pc/index/act/user.html');
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        }
    }

    public function vip_Notify()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('post.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }

        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!$POST['pay_no'] || md5($sign . $codepay_config['key']) != $POST['sign']) { //不合法的数据 KEY密钥为你的密钥
            exit('fail');
        } else { //合法的数据
            $out_trade_no = daddslashes($_GET['param']);
            //交易号
            $trade_no = daddslashes($_GET['pay_no']);
            $pay_order = Pays::findByOrderId($out_trade_no);
            if ($pay_order['status'] == 0) {
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $order = [
                    'type' => '' . $pay_order['type'] . '',
                    'orderid' => $out_trade_no,
                    'trade_no' => $trade_no,
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $pay_order['name'],
                    'money' => $pay_order['money'],
                    'status' => 2,
                    'zid' => WEB_ID,
                ];
                Order::add($order);
                $start_time = is_Vip_Day($pay_order['shopid']);
                //开通时间
                $vip_start = date("Y-m-d");
                //计算开通的vip时长
                if ($this->userInfo['viptime']) {
                    $vip_time = date("Y-m-d", strtotime("+" . $start_time . " day", strtotime($this->userInfo['viptime'])));
                } else {
                    $vip_time = date("Y-m-d", strtotime("+" . $start_time . " day"));
                }
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update([
                        'vipstart' => $vip_start,
                        'viptime' => $vip_time
                    ]);
            }
            exit('success');
        }
    }

    public function pe_Return()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('get.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }
        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!input('get.pay_no') || md5($sign . $codepay_config['key']) != input('get.sign')) { //不合法的数据 KEY密钥为你的密钥
            exit('验证失败！');
        } else { //合法的数据
            $out_trade_no = input('get.param');
            //交易号
            $trade_no = input('get.pay_no');
            $pay_order = Pays::findByOrderId($out_trade_no);
            $order = [
                'type' => '' . $pay_order['type'] . '',
                'orderid' => $out_trade_no,
                'trade_no' => $trade_no,
                'time' => date("Y-m-d H:i:s"),
                'name' => $pay_order['name'],
                'money' => $pay_order['money'],
                'status' => 2,
                'zid' => WEB_ID,
            ];
            if ($pay_order['status'] == 0) {
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $peie_Num = is_Peie_Num($pay_order['shopid']);
                $now_Num = $this->userInfo['peie'] + $peie_Num;
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update([
                        'peie' => $now_Num,
                    ]);
            } else {
                $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
                $this->assign('url', '/pc/index/act/user.html');
                if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
				}else{
					exit($this->fetch('pc/common/msg'));
				}
            }
            $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
            $this->assign('url', '/pc/index/act/user.html');
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        }
    }

    public function pe_Notify()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('post.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }

        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!$POST['pay_no'] || md5($sign . $codepay_config['key']) != $POST['sign']) { //不合法的数据 KEY密钥为你的密钥
            exit('fail');
        } else { //合法的数据
            $out_trade_no = daddslashes($_GET['param']);
            //交易号
            $trade_no = daddslashes($_GET['pay_no']);
            $pay_order = Pays::findByOrderId($out_trade_no);
            if ($pay_order['status'] == 0) {
                $order = [
                    'type' => '' . $pay_order['type'] . '',
                    'orderid' => $out_trade_no,
                    'trade_no' => $trade_no,
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $pay_order['name'],
                    'money' => $pay_order['money'],
                    'status' => 2,
                    'zid' => WEB_ID,
                ];
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $peie_Num = is_Peie_Num($pay_order['shopid']);
                $now_Num = $this->userInfo['peie'] + $peie_Num;
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update([
                        'peie' => $now_Num,
                    ]);
            }
            exit('success');
        }
    }

    public function rmb_Return()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('get.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }
        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!input('get.pay_no') || md5($sign . $codepay_config['key']) != input('get.sign')) { //不合法的数据 KEY密钥为你的密钥
            exit('验证失败！');
        } else { //合法的数据
            $out_trade_no = input('get.param');
            //交易号
            $trade_no = input('get.pay_no');
            $pay_order = Pays::findByOrderId($out_trade_no);
            $order = [
                'type' => '' . $pay_order['type'] . '',
                'orderid' => $out_trade_no,
                'trade_no' => $trade_no,
                'time' => date("Y-m-d H:i:s"),
                'name' => $pay_order['name'],
                'money' => $pay_order['money'],
                'status' => 2,
                'zid' => WEB_ID,
            ];
            if ($pay_order['status'] == 0) {
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $cz_money = $pay_order['money'];
                $res_money = $this->userInfo['money'] + $cz_money;
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->field('money')
                    ->update([
                        'money' => $res_money,
                    ]);
            } else {
                $this->assign('alert', '充值' . $pay_order['name'] . '成功，感谢您的购买');
                $this->assign('url', '/pc/index/act/user.html');
                if(check_mobile()){
					exit($this->fetch('mobile/common/msg'));
				}else{
					exit($this->fetch('pc/common/msg'));
				}
            }
            $this->assign('alert', '充值' . $pay_order['name'] . '成功，感谢您的购买');
            $this->assign('url', '/pc/index/act/user.html');
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        }
    }

    public function rmb_Notify()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('post.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }

        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!$POST['pay_no'] || md5($sign . $codepay_config['key']) != $POST['sign']) { //不合法的数据 KEY密钥为你的密钥
            exit('fail');
        } else { //合法的数据
            $out_trade_no = daddslashes($_GET['param']);
            //交易号
            $trade_no = daddslashes($_GET['pay_no']);
            $pay_order = Pays::findByOrderId($out_trade_no);
            if ($pay_order['status'] == 0) {
                $order = [
                    'type' => '' . $pay_order['type'] . '',
                    'orderid' => $out_trade_no,
                    'trade_no' => $trade_no,
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $pay_order['name'],
                    'money' => $pay_order['money'],
                    'status' => 2,
                    'zid' => WEB_ID,
                ];
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $cz_money = $pay_order['shopid'];
                $res_money = $this->userInfo['money'] + $cz_money;
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->field('money')
                    ->update([
                        'money' => $res_money,
                    ]);
            }
            exit('success');
        }
    }

    public function dl_Return()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('get.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }
        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!input('get.pay_no') || md5($sign . $codepay_config['key']) != input('get.sign')) { //不合法的数据 KEY密钥为你的密钥
            exit('验证失败！');
        } else { //合法的数据
            $out_trade_no = input('get.param');
            //交易号
            $trade_no = input('get.pay_no');
            $pay_order = Pays::findByOrderId($out_trade_no);
            $order = [
                'type' => '' . $pay_order['type'] . '',
                'orderid' => $out_trade_no,
                'trade_no' => $trade_no,
                'time' => date("Y-m-d H:i:s"),
                'name' => $pay_order['name'],
                'money' => $pay_order['money'],
                'status' => 2,
                'zid' => WEB_ID,
            ];
            if ($pay_order['status'] == 0) {
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $power = $pay_order['shopid'];
                //开通时间
                $vip_start = date("Y-m-d");
                //计算开通的vip时长
                if ($this->userInfo['viptime']) {
                    $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $power . '') . " day", strtotime($this->userInfo['viptime'])));
                } else {
                    $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $power . '') . " day"));
                }
                $give = [
                    'dlpower' => $power,
                    'vipstart' => $vip_start,
                    'viptime' => $vip_time,
                    'money' => $this->userInfo['money'] + config('sys_dl_give_rmb_' . $power . ''),
                    'dlzk' => config('sys_dl_give_z_' . $power . '')
                ];
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->field('dlpower,vipstart,viptime,money,dlzk')
                    ->update($give);
            } else {
                $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
                $this->assign('url', '/pc/index/act/user.html');
                if(check_mobile()){
					exit($this->fetch('mobile/common/msg'));
				}else{
					exit($this->fetch('pc/common/msg'));
				}
            }
            $this->assign('alert', '开通' . $pay_order['name'] . '成功，感谢您的购买');
            $this->assign('url', '/pc/index/act/user.html');
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        }
    }

    public function dl_Notify()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('post.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }

        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!$POST['pay_no'] || md5($sign . $codepay_config['key']) != $POST['sign']) { //不合法的数据 KEY密钥为你的密钥
            exit('fail');
        } else { //合法的数据
            $out_trade_no = daddslashes($_GET['param']);
            //交易号
            $trade_no = daddslashes($_GET['pay_no']);
            $pay_order = Pays::findByOrderId($out_trade_no);
            if ($pay_order['status'] == 0) {
                $order = [
                    'type' => '' . $pay_order['type'] . '',
                    'orderid' => $out_trade_no,
                    'trade_no' => $trade_no,
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $pay_order['name'],
                    'money' => $pay_order['money'],
                    'status' => 2,
                    'zid' => WEB_ID,
                ];
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $power = $pay_order['shopid'];
                //开通时间
                $vip_start = date("Y-m-d");
                //计算开通的vip时长
                if ($this->userInfo['viptime']) {
                    $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $power . '') . " day", strtotime($this->userInfo['viptime'])));
                } else {
                    $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $power . '') . " day"));
                }
                $give = [
                    'dlpower' => $power,
                    'vipstart' => $vip_start,
                    'viptime' => $vip_time,
                    'money' => $this->userInfo['money'] + config('sys_dl_give_rmb_' . $power . ''),
                    'dlzk' => config('sys_dl_give_z_' . $power . '')
                ];
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->field('dlpower,vipstart,viptime,money,dlzk')
                    ->update($give);
            }
            exit('success');
        }
    }

    public function fz_Return()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('get.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }
        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!input('get.pay_no') || md5($sign . $codepay_config['key']) != input('get.sign')) { //不合法的数据 KEY密钥为你的密钥
            exit('验证失败！');
        } else { //合法的数据
            $out_trade_no = input('get.param');
            //交易号
            $trade_no = input('get.pay_no');
            $pay_order = Pays::findByOrderId($out_trade_no);
            $order = [
                'type' => '' . $pay_order['type'] . '',
                'orderid' => $out_trade_no,
                'trade_no' => $trade_no,
                'time' => date("Y-m-d H:i:s"),
                'name' => $pay_order['name'],
                'money' => $pay_order['money'],
                'status' => 2,
                'zid' => WEB_ID,
            ];
            if ($pay_order['status'] == 0) {
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $is_kt_day = is_fz_add_day($pay_order['shopid']);//获取开通天数
                $kt_time = date("Y-m-d", strtotime("+" . $is_kt_day . " day"));
                $prefix = get_prefix() . '_';
                $addfz = [
                    'domain' => cookie('ktfz_url'),
                    'prefix' => $prefix,
                    'sup_id' => WEB_ID,
                    'web_key' => getRandStr(16),
                    'start_time' => date("Y-m-d"),
                    'user_id' => $this->userInfo['uid'],
                    'webname' => $pay_order['name'],
                    'user_qq' => $this->userInfo['qq'],
                    'mail' => $this->userInfo['mail'],
                    'end_time' => $kt_time
                ];
                Weblist::field('sup_id,user_id,webname,domain,user_qq,mail,start_time,end_time,prefix,web_key')->insert($addfz);
                $sqls = file_get_contents(ROOT_PATH . '/Other/site.sql');
                $sqls = str_replace('zy_', $prefix, $sqls);
                $explode = explode(';', $sqls);
                foreach ($explode as $sql) {
                    if ($sql = trim($sql)) {
                        Db::query($sql);
                    }
                }
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update(['power' => 9]);
            } else {
                $this->assign('alert', '开通分站：' . $pay_order['name'] . '成功，感谢您的购买');
                $this->assign('url', 'http://'.cookie('ktfz_url').'/');
                cookie('ktfz_url', null);
                if(check_mobile()){
					exit($this->fetch('mobile/common/msg'));
				}else{
					exit($this->fetch('pc/common/msg'));
				}
            }
            $this->assign('alert', '开通分站：' . $pay_order['name'] . '成功，感谢您的购买');
            $this->assign('url', 'http://'.cookie('ktfz_url').'/');
            cookie('ktfz_url', null);
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        }
    }

    public function fz_Notify()
    {
        require ROOT_PATH . 'Other/codepay/codepay_config.php';
        $GET = input('post.');
        ksort($GET); //排序get参数
        reset($GET); //内部指针指向数组中的第一个元素
        $sign = '';
        foreach ($GET AS $key => $val) {
            if ($val == '') continue;
            if ($key != 'sign') {
                if ($sign != '') {
                    $sign .= "&";
                }
                $sign .= "$key=$val"; //拼接为url参数形式
            }
        }

        if (config('sys_is_pay') != 1) {
            exit('fail');
        } elseif (!$POST['pay_no'] || md5($sign . $codepay_config['key']) != $POST['sign']) { //不合法的数据 KEY密钥为你的密钥
            exit('fail');
        } else { //合法的数据
            $out_trade_no = daddslashes($_GET['param']);
            //交易号
            $trade_no = daddslashes($_GET['pay_no']);
            $pay_order = Pays::findByOrderId($out_trade_no);
            if ($pay_order['status'] == 0) {
                $order = [
                    'type' => '' . $pay_order['type'] . '',
                    'orderid' => $out_trade_no,
                    'trade_no' => $trade_no,
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $pay_order['name'],
                    'money' => $pay_order['money'],
                    'status' => 2,
                    'zid' => WEB_ID,
                ];
                Order::add($order);
                Pays::updateByOrderId($out_trade_no, ['status' => 2, 'endtime' => date("Y-m-d H:i:s")]);
                $is_kt_day = is_fz_add_day($pay_order['shopid']);//获取开通天数
                $kt_time = date("Y-m-d", strtotime("+" . $is_kt_day . " day"));
                $prefix = get_prefix() . '_';
                $addfz = [
                    'domain' => cookie('ktfz_url'),
                    'prefix' => $prefix,
                    'sup_id' => WEB_ID,
                    'web_key' => getRandStr(16),
                    'start_time' => date("Y-m-d"),
                    'user_id' => $this->userInfo['uid'],
                    'webname' => $pay_order['name'],
                    'user_qq' => $this->userInfo['qq'],
                    'mail' => $this->userInfo['mail'],
                    'end_time' => $kt_time
                ];
                Weblist::field('sup_id,user_id,webname,domain,user_qq,mail,start_time,end_time,prefix,web_key')->insert($addfz);
                $sqls = file_get_contents(ROOT_PATH . '/Other/site.sql');
                $sqls = str_replace('zy_', $prefix, $sqls);
                $explode = explode(';', $sqls);
                foreach ($explode as $sql) {
                    if ($sql = trim($sql)) {
                        Db::query($sql);
                    }
                }
                //修改用户信息
                Users::where('uid', '=', $this->userInfo['uid'])
                    ->update(['power' => 9]);
            }
            exit('success');
        }
    }

    protected
    function _initialize()
    {
        parent::_initialize();
        $this->checkLogin();
		
    }
	
	private
    function checkLogin()
    {
        $check_Login = Users::checkLogin();
        if ($check_Login != null) {
            $this->assign('alert', $check_Login['msg']);
            $this->assign('url', $check_Login['url']);
            if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        } else {
            $this->userInfo = Users::getLoginUser();
            $this->assign('userInfo', $this->userInfo);

        }
    }
}